package com.youran.restful.webprinter;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 程序启动入口，采用springboot方式启动
 * 
 * 自动扫描这个类的目录及其子目录
 * 
 * @author DAL.feng
 */
@SpringBootApplication
@RestController
public class AppRun {

	private static Logger logger = LoggerFactory.getLogger(AppRun.class);
	
	public static void main(String[] args) {
		logger.info("进程准备启动…… {}", args.length);
		for(String s : args) {
			logger.info("arg={}", s);
		}
		SpringApplication.run(AppRun.class, args);
//		SpringApplication app = new SpringApplication(AppRun.class);  
//        app.setWebEnvironment(true);
//          
//        Set<Object> set = new HashSet<Object>();  
//        set.add("classpath:appContext-service.xml");  
//        app.setSources(set);  
//        app.run(args);
		logger.info("进程已启动！");
	}

	/**
	 * 解决silverlight跨域问题；
	 * 在silverlight进行跨域访问时，会先访问服务的根目录下clientaccesspolicy.xml或crossdomain.xml文件，获取访问权限；
	 * 这里模拟根目录下有此文件进行文本返回。
	 * 
	 * @return
	 */
	@RequestMapping(value = {"/clientaccesspolicy.xml"}, method={RequestMethod.GET,RequestMethod.POST})
	public String clientaccesspolicy () {
		StringBuilder info = new StringBuilder();
		info.append("<?xml version='1.0' encoding='utf-8'?>")
			.append("<access-policy>")
			.append("<cross-domain-access>")
			.append("<policy>")
			.append("<allow-from>")
			.append("<domain uri='*'/>")
			.append("</allow-from>")
			.append("<grant-to>")
			.append("<resource path='/' include-subpaths='true'/>")
			.append("</grant-to>")
			.append("</policy>")
			.append("</cross-domain-access>")
			.append("</access-policy>");
		return info.toString();
	}

	/**
	 * 解决silverlight跨域问题；
	 * 在silverlight进行跨域访问时，会先访问服务的根目录下clientaccesspolicy.xml或crossdomain.xml文件，获取访问权限；
	 * 这里模拟根目录下有此文件进行文本返回。
	 * 
	 * @return
	 */
	@RequestMapping(value = {"/crossdomain.xml"}, method={RequestMethod.GET,RequestMethod.POST})
	public String crossdomain () {
		StringBuilder info = new StringBuilder();
		info.append("<?xml version='1.0' encoding='utf-8'?>")
			.append("<cross-domain-policy>")
			.append("<allow-access-from domain='*' />")
			.append("</cross-domain-policy>");
		return info.toString();
	}
}
